package com.css.fxfzfxqh.modules.modelManagement.repository.impl;

import com.css.fxfzfxqh.modules.modelManagement.entity.CasualtyCalculationEntity;
import com.css.fxfzfxqh.modules.modelManagement.param.PagePersonnelCasualtiesParam;
import com.css.fxfzfxqh.modules.modelManagement.repository.PersonnelCasualtiesRepository;
import com.css.fxfzfxqh.modules.modelManagement.vo.PagePersonnelCasualtiesVo;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import com.css.fxfzfxqh.zcpt.sys.service.SysAreaService;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class PersonnelCasualtiesRepositoryImpl implements PersonnelCasualtiesRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private SysAreaService areaService;


    @Override
    public int getSizeByName(String modelName) {
        String sql = "select count(id) from fxqh_casualty_calculation where model_name = '"+modelName+"' and del_flag = '0'";
        try {
            return jdbcTemplate.queryForObject(sql,Integer.class) ;
        } catch (DataAccessException e) {
            return 0;
        }
    }

    @Override
    public void save(CasualtyCalculationEntity entity) {
//        jdbcTemplate.update("insert into fxqh_casualty_calculation(id,model_name,smrs_number1,smrs_number2,injuries,is_default,province,city,create_user_id,create_user_name,create_time,org_id,org_name,del_flag,death_number) " +
//                        "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", entity.getId(), entity.getModelName(), entity.getSmrsNumber1(), entity.getSmrsNumber2(),
//                entity.getInjuries(), entity.getIsDefault(), entity.getProvince(), entity.getCity(), entity.getCreateUserId(), entity.getCreateUserName(), entity.getCreateTime(),
//                entity.getOrgId(), entity.getOrgName(), entity.getDelFlag(), entity.getDeathNumber());
        jdbcTemplate.update("insert into fxqh_casualty_calculation  (id,model_name,is_default,province,city,create_user_id,create_user_name,create_time,update_user_id,update_time,org_id,org_name,del_flag,night_class1,night_class2,night_class3,day_class1,day_class2,day_class3,intact_injury,intact_death,slight_injury,slight_death,medium_injury,medium_death,serious_injury,serious_death,destroy_injury,destroy_death ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )",
                entity.getId(),entity.getModelName(),entity.getIsDefault(),entity.getProvince(),entity.getCity(),entity.getCreateUserId(),entity.getCreateUserName(),entity.getCreateTime(),entity.getUpdateUserId(),entity.getUpdateTime(),entity.getOrgId(),entity.getOrgName(),entity.getDelFlag(),entity.getNightClass1(),entity.getNightClass2(),entity.getNightClass3(),entity.getDayClass1(),entity.getDayClass2(),entity.getDayClass3(),entity.getIntactInjury(),entity.getIntactDeath(),entity.getSlightInjury(),entity.getSlightDeath(),entity.getMediumInjury(),entity.getMediumDeath(),entity.getSeriousInjury(),entity.getSeriousDeath(),entity.getDestroyInjury(),entity.getDestroyDeath());
    }

    @Override
    public Map<String, Object> page(PagePersonnelCasualtiesParam param, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from fxqh_casualty_calculation where del_flag = '0'");
        if (!PlatformObjectUtils.isEmpty(param.getModelName())) {
            sql.append(" and model_name like '%" + param.getModelName() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getProvince())) {
            if ("cd726776c7de41e4a41dcdb3d90b2806".equals(param.getProvince()) || "225ac763ab714524b87d95140a93a4a5".equals(param.getProvince()) || "8a3f7d3f78ad456f92f487a979091c7f".equals(param.getProvince()) || "afcbf07008a34c2e85917cfd34008033".equals(param.getProvince())) {
                //拿到北京上海天津重庆四省的省id
                String pid = areaService.getParentId(param.getProvince());
                param.setProvince(pid);
            }
            sql.append(" and province='").append(param.getProvince()).append("'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getCity())) {
            sql.append(" and (position('").append(param.getCity()).append("' in city) > 0 ");
            sql.append(" or city='全部')");
        }
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(PagePersonnelCasualtiesVo.class)).size();
        StringBuilder limitSql = new StringBuilder(" limit " + pageSize + " offset " + (curPage - 1) * pageSize);
        StringBuilder orderSql = new StringBuilder(" order by is_default desc, create_time desc");
        sql.append(orderSql).append(limitSql);
        List<PagePersonnelCasualtiesVo> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(PagePersonnelCasualtiesVo.class));
        HashMap<String, Object> resultMap = new HashMap();
        resultMap.put("total", total);
        resultMap.put("rows", list);
        return resultMap;
    }

    @Override
    public void deleteById(String id) {
        String sql = "update fxqh_casualty_calculation set del_flag = '1' where id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void updateById(CasualtyCalculationEntity entity) {
        jdbcTemplate.update("update  fxqh_casualty_calculation  set model_name=?,update_user_id=?,update_time=?,night_class1=?,night_class2=?,night_class3=?,day_class1=?,day_class2=?,day_class3=?,intact_injury=?,intact_death=?,slight_injury=?,slight_death=?,medium_injury=?,medium_death=?,serious_injury=?,serious_death=?,destroy_injury=?,destroy_death=?"
                        +" where id=?",
                entity.getModelName(),entity.getUpdateUserId(),entity.getUpdateTime(),entity.getNightClass1(),entity.getNightClass2(),entity.getNightClass3(),entity.getDayClass1(),entity.getDayClass2(),entity.getDayClass3(),entity.getIntactInjury(),entity.getIntactDeath(),entity.getSlightInjury(),entity.getSlightDeath(),entity.getMediumInjury(),entity.getMediumDeath(),entity.getSeriousInjury(),entity.getSeriousDeath(),entity.getDestroyInjury(),entity.getDestroyDeath(),
                entity.getId()
        );
    }

    @Override
    public CasualtyCalculationEntity getById(String id) {
        String sql = "select * from fxqh_casualty_calculation where id = '"+id+"'";
        try {
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(CasualtyCalculationEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }
}
